package com.ihome.android.market2.asynctask;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.ihome.android.EAApp;
import com.ihome.android.academy.DetailActivity;
import com.ihome.android.academy.RankingActivity;
import com.ihome.android.academy.WebViewActivity;
import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.market2.frag.AlbumAppListFragment;
import com.ihome.android.market2.frag.CategoryAppFragment;
import com.ihome.android.market2.frag.RecommAppFragment;
import com.ihome.android.market2.frag.SearchAppFragment;
import com.ihome.android.market2.info.AppDetailInfo;
import com.ihome.android.market2.view.DialogFactory;
import com.ihome.android.market2.view.MyProgressDialog;
import com.ihome.android.market2.view.DialogFactory.onBtnClickListener;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;
import com.ihome.android.util.log.UserBehaviorLog;
import com.ihome.android.util.log.UserBehaviorLog.NotifyType;
import com.ihome.android.util.log.UserBehaviorLog.UserBehavior;

/**
 * 获取是否能够显示应用
 * 
 * @author Emerson
 * 
 */
public class GetViewAppDetailTask extends AsyncTask<Bundle, Integer, Bundle> {
	private final String		TAG	= GetViewAppDetailTask.class.getSimpleName();
	private Context				context;
	private MyProgressDialog	progressDialog;
	private AppDetailInfo		mAppDetailInfo;
	
	private Fragment			fragment;
	private int 				appId;


	public GetViewAppDetailTask(Context context,int appId) {
		this.context = context;
		this.appId = appId;
	}

	public GetViewAppDetailTask(Context context,Fragment fragment,int appId) {
		this.context = context;
		this.fragment = fragment;
		this.appId   = appId;
	}
	
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if ((context instanceof Activity))
			return;
		if((fragment instanceof Fragment)){
			return;
		}
		progressDialog = MyProgressDialog.getDialog(context, context.getString(R.string.user_grabing_data));
		progressDialog.show();
		progressDialog.setCancelable(true);
		progressDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (GetViewAppDetailTask.this.getStatus().compareTo(AsyncTask.Status.FINISHED) != 0)
					GetViewAppDetailTask.this.cancel(true);
			}
		});
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = params[0];
		int appId = b.getInt(HttpKeys.KEY_APPID);
		String pathId = b.getString(HttpKeys.KEY_PATHID);
		LogUtil.v(TAG, "appId = " + appId + "; pathId = " + pathId);
		if (appId <= 0) {
			return b;
		}

		try {
			String appinfo = new HttpActions(context).getAppInfo(appId, pathId);
			LogUtil.v(TAG, "appinfo = " + appinfo);
			mAppDetailInfo = JsonAnalytic.getInstance().anslyseAppDetailResult(appinfo);
			if (mAppDetailInfo != null) {
				LogUtil.v(TAG, "mAppDetailInfo =  \n " + mAppDetailInfo.toString());
			}
			else {
				cancel(true);
			}

		} catch (ITVException e) {
			String[] msg1 = { "", "", "" , "", "app.getInfo", "" };
			UserBehaviorLog.getInstance().writeLog(UserBehavior.STORERUN_EXCEPTION, NotifyType.TYPE_FAILURE, "", msg1);
			e.printStackTrace();
			LogUtil.e(TAG, e.toString());
			b.putInt(HttpKeys.KEY_CODE, e.getStatusCode());
			b.putString(HttpKeys.KEY_DESCRIPTION, e.getMessage());
		}
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);
		if (progressDialog != null)
			progressDialog.dismiss();

		/** 请求出错处理 */
		if (result.containsKey(HttpKeys.KEY_CODE)) {
			if(ITVException.CODE_LOGINOUT_THREE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGINOUT_ONE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_SESSIONKEY == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGIN_ONE == result.getInt(HttpKeys.KEY_CODE)){
				new DialogFactory(context).showDialog("",result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")",context.getString(R.string.dialog_ok),new onBtnClickListener() {
					
					@Override
					public void btnRightClickListener(View v) {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void btnNeutralClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void btnLeftClickListener(View v) {
						// TODO Auto-generated method stub
						EAApp.getApp().exitApp(true);
					}
					
					@Override
					public void btnCloseClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
				});
				return;
			}
			
			if(result.getInt(HttpKeys.KEY_CODE)==-88101)
				new DialogFactory(context).showToast(result.getString(HttpKeys.KEY_DESCRIPTION), 3000);
			return;
		}


		if (RankingActivity.class.isInstance(context) && mAppDetailInfo != null) {
			// 填充AppDetailInfo
			((RankingActivity) context).toDetailPage(appId);
		}
		
		if (WebViewActivity.class.isInstance(context) && mAppDetailInfo != null) {
			// 填充AppDetailInfo
//			((WebViewActivity) context).toDetailPage(appId);
		}
		
		if (mAppDetailInfo != null && fragment instanceof AlbumAppListFragment) {
			((AlbumAppListFragment) fragment).toDetailPage(appId);
		}
		
		if (mAppDetailInfo != null && fragment instanceof CategoryAppFragment) {
			((CategoryAppFragment) fragment).toDetailPage(appId);
		}
		
		if (mAppDetailInfo != null && fragment instanceof SearchAppFragment) {
			((SearchAppFragment) fragment).toDetailPage(appId);
		}
		
		if (mAppDetailInfo != null && fragment instanceof RecommAppFragment) {
			((RecommAppFragment) fragment).toDetailPage(appId);
		}
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");
		if (progressDialog != null)
			progressDialog.dismiss();

		if (context instanceof DetailActivity)
			((DetailActivity) context).finish();

	}

}
